package club.haomoon.boss.service.impl;

import club.haomoon.boss.service.ISettingCopyrightService;
import club.haomoon.boss.validate.base.SettingCopyrightValidate;
import club.haomoon.boss.vo.base.SettingCopyrightVo;
import club.haomoon.core.util.ConfigUtil;
import club.haomoon.core.util.ListUtil;
import com.alibaba.fastjson2.JSON;

import org.springframework.stereotype.Service;

import java.util.*;

/**
 * -------------------------
 * @Description: 网站备案服务实现类
 * @Author: Wangjianwei
 * @Date: 2025/02/17 16:25
 * -------------------------
 */
@Service
public class SettingCopyrightServiceImpl implements ISettingCopyrightService {

    /**
     * 获取网站备案信息
     * @return List<SettingCopyrightVo>
     */
    @Override
    public List<SettingCopyrightVo> getCopyright() {
        String config = ConfigUtil.get("copyright", "config", "[]");
        List<Map<String, String>> copyright = ListUtil.stringToListAsMapStr(config);

        List<SettingCopyrightVo> list = new LinkedList<>();
        for (Map<String, String> map : copyright) {
            SettingCopyrightVo vo = new SettingCopyrightVo();
            vo.setKey(map.getOrDefault("key", ""));
            vo.setValue(map.getOrDefault("value", ""));
            list.add(vo);
        }

        return list;
    }

    /**
     * 保存网站备案信息
     * @param copyrightValidate 参数
     */
    @Override
    public void setCopyright(SettingCopyrightValidate copyrightValidate) {
        String s = JSON.toJSONString(copyrightValidate.getConfig());
        ConfigUtil.set("copyright", "config", s);
    }

}
